package com.dengyiming.item.web;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dengyiming.common.dto.PageDTO;
import com.dengyiming.item.pojo.Item;
import com.dengyiming.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 邓懿鸣
 */
@RestController
@RequestMapping("item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("list")
    public PageDTO<Item> itemList(@RequestParam(value = "page") Integer page,
                                  @RequestParam(value = "size") Integer size) {
        log.info("当前页{}当前页数据条数{}", page, size);
        IPage<Item> page1 = new Page(page, size);
        itemService.page(page1, null);
        PageDTO pageDTO = new PageDTO();
        pageDTO.setTotal(page1.getTotal());
        pageDTO.setList(page1.getRecords());
        return pageDTO;
    }

    /**
     * 上/下架商品
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/status/{id}/{status}")
    public void itemStatus(@PathVariable Long id, @PathVariable Integer status) {
        log.info("要下架的商品id和状态：{} {}",id,status);

        itemService.itemStatus(id,status);
    }

    /**
     * 新增商品
     *
     * @param item
     * @return
     */
    @PostMapping
    public Boolean itemInsert(@RequestBody Item item) {
        log.info("新增商品商品{}",item);
        return itemService.save(item);
    }

    /**
     * 修改商品
     *
     * @param item
     * @return
     */
    @PutMapping
    public Boolean itemUpdate(@RequestBody Item item) {
        log.info("修改商品的数据：{}",item);
        return itemService.updateById(item);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Boolean itemId(@PathVariable Long id) {
        log.info("要删除的商品的id:{}",id);
        return itemService.removeById(id);
    }
    @GetMapping("{id}")
    public Item itemById(@PathVariable Long id){
        return itemService.getById(id);
    }
}
